package com.example.stars_project_android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.stars_project_android.R;
import com.example.stars_project_android.entity.GuestBookEntity;
import com.sackcentury.shinebuttonlib.ShineButton;

import java.util.List;

/**
 * 留言板适配器
 */

public class GuestBookAdapter extends BaseAdapter {
    private Context mContext;
    private int layoutResId;
    private List<GuestBookEntity> GuestBookList;
    private boolean shineBtn = false;

    public GuestBookAdapter(Context mContext, int layoutResId, List<GuestBookEntity> GuestBookList) {
        this.mContext = mContext;
        this.layoutResId = layoutResId;
        this.GuestBookList = GuestBookList;
    }

    @Override
    public int getCount() {
        if(GuestBookList != null){
            return GuestBookList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(GuestBookList != null){
            return GuestBookList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            //加载item的布局文件，生成一个View对象，再给convertView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.userImg = convertView.findViewById(R.id.iv_user_img);
            viewHolder.userId = convertView.findViewById(R.id.tv_userId);
            viewHolder.gridView = convertView.findViewById(R.id.grid);
//            viewHolder.img = convertView.findViewById(R.id.iv_img);
            viewHolder.userMessage = convertView.findViewById(R.id.tv_message);
            viewHolder.messageTime = convertView.findViewById(R.id.tv_message_time);
            viewHolder.CommentsNum = convertView.findViewById(R.id.tv_comments_number);
            viewHolder.likeNum = convertView.findViewById(R.id.tv_like_number);
            viewHolder.likeBtn = convertView.findViewById(R.id.btn_heart);
            //将ViewHolder对象同convertView关联起来
            convertView.setTag(viewHolder);
        }else {//重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (GuestBookAdapter.ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        viewHolder.userImg.setImageResource(GuestBookList.get(position).getUserImg());
        viewHolder.userId.setText(GuestBookList.get(position).getUserId());
        viewHolder.gridView.setAdapter(new GridGuestAdapter(
                convertView.getContext(),
                R.layout.grid_guest,
                GuestBookList.get(position).getImg()
        ));
        viewHolder.userMessage.setText(GuestBookList.get(position).getUserMessage()+"");
        viewHolder.messageTime.setText(GuestBookList.get(position).getMessageTime());
        viewHolder.CommentsNum.setText(GuestBookList.get(position).getCommentsNum());
        viewHolder.likeNum.setText(GuestBookList.get(position).getLikeNum());
        viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shineBtn = false){
                    viewHolder.likeNum.setText((Integer.valueOf(GuestBookList.get(position).getLikeNum().toString()) - 1) + "");
                    shineBtn = true;
                }else{
                    viewHolder.likeNum.setText((Integer.valueOf(GuestBookList.get(position).getLikeNum().toString()) + 1) + "");
                    shineBtn = false;
                }
            }
        });
        return convertView;
    }

    private ConcernStarListAdapter.onItemDeleteListener mOnItemDeleteListener;

    public void setOnItemDeleteClickListener(ConcernStarListAdapter.onItemDeleteListener mOnItemDeleteListener) {
        this.mOnItemDeleteListener = mOnItemDeleteListener;
    }

    private class ViewHolder {
        ImageView userImg;//用户头像
        TextView userId;//用户id
        GridView gridView;
        TextView userMessage;//用户留言
        TextView messageTime;//发布时间
        TextView CommentsNum;//留言数
        TextView likeNum;//点赞数
        ShineButton likeBtn;//点赞按钮
    }
}
